<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      title="个人信息"
      left-arrow
      class="page-nav-bar"
      @click-left="$router.back()"
    />
    <!-- 四个操作按钮 -->
    <input ref="myFile" type="file" style="display: none" @change="chooseImg" />
    <van-cell title="头像" is-link @click="$refs.myFile.click()">
      <van-image class="avatar" fit="cover" round :src="userInfo.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="userInfo.name"
      is-link
      @click="isShowName = true"
    />
    <van-cell
      title="性别"
      :value="userInfo.gender ? '女' : '男'"
      is-link
      @click="isShowGender = true"
    />
    <van-cell
      title="生日"
      :value="userInfo.birthday"
      is-link
      @click="isShowBirthday = true"
    />

    <!-- 修改头像弹框 -->
    <van-popup
      v-model="isShowAuatar"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <UpdateAvatar
        :userInfo="userInfo"
        @closePop="isShowAuatar = $event"
        :imgData="imgData"
      />
    </van-popup>

    <!-- 修改昵称弹框 -->
    <van-popup
      v-model="isShowName"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <UpdateName :userInfo="userInfo" @closePop="isShowName = $event" />
    </van-popup>

    <!-- 修改性别弹框 -->
    <van-popup
      v-model="isShowGender"
      position="bottom"
      :style="{ height: '45%' }"
    >
      <UpdateGender :userInfo="userInfo" @closePop="isShowGender = $event" />
    </van-popup>

    <!-- 修改生日弹框 -->
    <van-popup
      v-model="isShowBirthday"
      position="bottom"
      :style="{ height: '45%' }"
    >
      <UpdateBirthday
        :userInfo="userInfo"
        @closePop="isShowBirthday = $event"
      />
    </van-popup>
  </div>
</template>

<script>
// 导入 用户信息API
import { getProfile } from '@/api/user'
// 导入组件
import UpdateAvatar from './components/update-avatar.vue'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
export default {
  name: 'Profile',
  components: { UpdateAvatar, UpdateName, UpdateGender, UpdateBirthday },

  data () {
    return {
      userInfo: [],
      isShowAuatar: false,
      isShowName: false,
      isShowGender: false,
      isShowBirthday: false,
      imgData: ''
    }
  },

  mounted () {},

  methods: {
    chooseImg () {
      if (this.$refs.myFile.files.length === 0) return
      // 文件选择框的 files属性，返回一个包含选中图信息的伪数组
      this.imgData = URL.createObjectURL(this.$refs.myFile.files[0])
      console.log(this.$refs.myFile.files[0])
      // 打开修改头像弹出框
      this.isShowAuatar = true
    }
  },

  async created () {
    // 请求用户详细信息
    const res = await getProfile()
    console.log('个人资料', res)
    // 挂载到data中
    this.userInfo = res.data.data
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
/deep/ .van-nav-bar__content {
  background-color: #0094ff;
  // .van-nav-bar__text {
  //   color: #fff;
  // }
  .van-nav-bar__title {
    color: #fff;
  }
  .van-icon-arrow-left {
    color: #fff;
  }
}
</style>
